package example.strategy.symmetry;

import org.apache.log4j.Logger;
import com.scop.desensitize.encry.rreversible.symmetry.AbstractSymmetryEncry;
import com.scop.desensitize.exception.strategy.DecodeException;
import com.scop.desensitize.exception.strategy.EncodeException;
import com.scop.desensitize.strategy.rreversible.symmetry.ThreeDesStrategy;

import java.security.NoSuchAlgorithmException;

/**
 * @desc des3策略类的使用示例
 */
public class ThreeDesStrategyDemo {

	public static Logger log = Logger.getLogger(ThreeDesStrategyDemo.class);

	public static void main(String[] args) throws EncodeException, DecodeException {
		String data = "scop";
		defaultEncry(data);
		//customEncry(data);
	}

	/**
	 * @desc 自定义偏移量，密码
	 */
	public static void customEncry(String data) throws EncodeException, DecodeException {
		AbstractSymmetryEncry des3= ThreeDesStrategy.getInstance();
		des3.setIV_PARAMETER("12332121");
		StringBuffer sb=new StringBuffer();
		//密码必须大于等于8位
		for (int i = 0; i < 24; i++) {
			sb.append("2");
		}
		des3.setPassword(sb.toString());
		String encodeStr =(String) des3.encode(data);
		String decodeStr =(String) des3.decode(encodeStr);

		log.info("自定义密码加密--->默认数据：" + data);
		log.info("自定义密码加密--->加密之后：" + encodeStr);
		log.info("自定义密码加密--->解密之后：" + decodeStr);
	}

	/**
	 * @desc 默认执行
	 * @throws NoSuchAlgorithmException
	 */
	public static void defaultEncry(String data) throws EncodeException, DecodeException {
		AbstractSymmetryEncry des3=ThreeDesStrategy.getInstance();

		String encodeStr =(String) des3.encode(data);
		String decodeStr =(String) des3.decode(encodeStr);

		log.info("默认密码加密--->默认数据：" + data);
		log.info("默认密码加密--->加密数据：" + encodeStr);
		log.info("默认密码加密--->解密数据：" + decodeStr);
	}
}
